package com.dinian.zeus.service.impl;

import com.dinian.zeus.domain.Newspaper;
import com.dinian.zeus.domain.Periodical;
import com.dinian.zeus.domain.ResponseVO;
import com.dinian.zeus.mapper.NewspaperMapper;
import com.dinian.zeus.mapper.PeriodicalMapper;
import com.dinian.zeus.service.NewspaperService;
import com.dinian.zeus.service.PeriodicalService;
import com.dinian.zeus.util.Contant;
import com.dinian.zeus.util.DateUtil;
import com.dinian.zeus.util.ExcelUtil;
import com.dinian.zeus.util.UploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author dinian
 * @Date 2018-12-20
 * 期刊Service
 */
@Slf4j
@Service
public class PeriodicalServiceImpl implements PeriodicalService {

    @Autowired
    private PeriodicalMapper periodicalMapper;

    /**
     * 查询所有期刊信息
     * @return
     */
    @Override
    public List<Periodical> findAllPeriodical() {
        List<Periodical> periodicals = periodicalMapper.selectPeriodicals();
        log.info("init periodical totalNum: {}",periodicals.size());
        return periodicals;
    }

    /**
     * 根据查询条件搜索期刊信息
     * @param periodical
     * @return
     */
    @Override
    public List<ResponseVO> searchPeriodical(Periodical periodical) {
        if(periodical == null){
            return null;
        }
        List<ResponseVO> periodicals = periodicalMapper.selectPeriodicalsByConditions(periodical);
        log.info("search periodical totalNum: {}",periodicals.size());
        return periodicals;
    }

    /**
     * 批量导入
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public Integer importPeriodical(MultipartFile file) throws Exception {
        Boolean  isUpload = UploadUtil.file(file);
        if (isUpload){
            List<Periodical> periodicals = ExcelUtil.importData(
                    Contant.WIN_PATH + file.getOriginalFilename(),Periodical.class,2);
            log.debug("import newspapers totalNum: {}",periodicals.size());
            for (Periodical periodical : periodicals){
                periodical.setCreateTime(DateUtil.formatDate());
                periodical.setUpdateTime(DateUtil.formatDate());
                periodicalMapper.insertPeriodical(periodical);
            }
            UploadUtil.deleteDir();
            return periodicals.size();
        }
        return 0;
    }

    /**
     * 单条添加
     * @param periodical
     * @return
     */
    @Override
    public int addPeriodical(Periodical periodical) {
        log.debug(periodical.toString());
        periodical.setCreateTime(DateUtil.formatDate());
        periodical.setUpdateTime(DateUtil.formatDate());
        return periodicalMapper.insertPeriodical(periodical);
    }

    /**
     * 查询byID
     * @param id
     * @return
     */
    @Override
    public Periodical queryPeriodicalById(String id) {
        log.debug("query id= {}",id);
        Periodical periodical = periodicalMapper.selectPeriodicalById(Integer.parseInt(id));
        return periodical;
    }

    /**
     * 修改
     * @param periodical
     */
    @Override
    public void modifyPeriodical(Periodical periodical) {
        log.debug("update periodical",periodical);
        periodical.setUpdateTime(DateUtil.formatDate());
        periodicalMapper.updatePeriodical(periodical);
    }

    /**
     * 删除
     * @param ids
     */
    @Override
    public void removePeriodicals(Integer[] ids) {
        log.debug(ids.toString());
        periodicalMapper.deletePeriodicalByIds(ids);
    }
}
